package com.soms.util;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class HttpResponseUtil {

    /**
     * HTML的contentType格式
     */
    private static final String HTML = "text/html;charset=utf-8";
    /**
     * JSON的contentType格式
     */
    private static final String JSON = "application/json;charset=utf-8";

    /**
     * 响应一段html文本
     * @param response
     * @param html html文本内容
     * @throws IOException
     */
    public static void writeHtml(HttpServletResponse response, String html) throws IOException {
        response.setContentType(HTML);
        PrintWriter writer = response.getWriter();
        writer.write(html);
        writer.flush();
        writer.close();
    }

    /**
     * 响应JSON数据
     * @param response
     * @param json  JSON格式的字符串
     * @throws IOException
     */
    public static void writeJson(HttpServletResponse response, String json) throws IOException {
        response.setContentType(JSON);
        PrintWriter writer = response.getWriter();
        writer.write(json);
        writer.flush();
        writer.close();
    }
}